/*
 * test_startsvc.cpp
 *
 *  Created on: Apr 21, 2014
 *      Author: killerg
 */


/*
 * ./test s001 -d // run as deamon
 * ./test s001 -s // status
 * ./test s001 -f // finish
 * ./test s001 // just start
 * initing pid
 * running pid
 * down -1
 * lost -1
 */

#include "share/util.h"



#include <fcntl.h>           /* For O_* constants */
static int my_open(const char* path) {
	int fd = open(
			fmt_cstr("%s/%s", get_deploy(), path),
			O_CREAT|O_WRONLY|O_TRUNC,
			S_IWUSR|S_IRUSR|S_IRGRP);
	if (fd < 0)
		perror("my_open");

	return fd;
}

void run(SvcData* data) {
	std::string path = fmt_cstr("bin/test_log_%s", data->prefix);
	int logfd = my_open(path.c_str());
	if (logfd < 0) {
		data->status = SvcData::Shutting;
		return;
	}

	data->status = SvcData::Initing;
	sleep(1);

	data->status = SvcData::Running;
	while (!data->shut) {
		data->refresh = time(NULL);
		sleep(1);
	}

	data->status = SvcData::Shutting;
	sleep(1);
}
#include <assert.h>
bool test_startsvc(int argc, char** argv) {
	return svc_ctrl(argc, argv, run);
}
